<?php

namespace Core\Common\Base;

use Core\Common\Route\Request;

if (PHP_OS == 'Darwin') {
    !defined('EINVAL') && define('EINVAL', 22);/* Invalid argument */
    !defined('EPIPE') && define('EPIPE', 32);/* Broken pipe */
    !defined('EAGAIN') && define('EAGAIN', 35);/* Resource temporarily unavailable */
    !defined('EINPROGRESS') && define('EINPROGRESS', 36);/* Operation now in progress */
    !defined('EWOULDBLOCK') && define('EWOULDBLOCK', EAGAIN);/* Operation would block */
    !defined('EADDRINUSE') && define('EADDRINUSE', 48);/* Address already in use */
    !defined('ECONNRESET') && define('ECONNRESET', 54);/* Connection reset by peer */
    !defined('ETIMEDOUT') && define('ETIMEDOUT', 60);/* Connection timed out */
    !defined('ECONNREFUSED') && define('ECONNREFUSED', 61);/* Connection refused */
} elseif (PHP_OS == 'Linux') {
    !defined('EINVAL') && define('EINVAL', 22);/* Invalid argument */
    !defined('EPIPE') && define('EPIPE', 32);/* Broken pipe */
    !defined('EAGAIN') && define('EAGAIN', 11);/* Resource temporarily unavailable */
    !defined('EINPROGRESS') && define('EINPROGRESS', 115);/* Operation now in progress */
    !defined('EWOULDBLOCK') && define('EWOULDBLOCK', EAGAIN);/* Operation would block */
    !defined('EADDRINUSE') && define('EADDRINUSE', 98);/* Address already in use */
    !defined('ECONNRESET') && define('ECONNRESET', 104);/* Connection reset by peer */
    !defined('ETIMEDOUT') && define('ETIMEDOUT', 110);/* Connection timed out */
    !defined('ECONNREFUSED') && define('ECONNREFUSED', 111);/* Connection refused */
}

/**
 * Description of Bootstrap
 *
 * @author baiyang <baiyang@ibantang.com>
 */
class Bootstrap
{
    /**
     * @param $documentRoot
     * @param string $name
     * 获取配置文件和启动环境，先判断命令行参数
     */
    public static function startUp($documentRoot)
    {
        define('CORE_WORK_ROOT', rtrim($documentRoot, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);
        define('CORE_APP_PATH', CORE_WORK_ROOT . 'app' . DIRECTORY_SEPARATOR);
        define('CORE_CONTROLLER_DIR', CORE_APP_PATH . 'Controller' . DIRECTORY_SEPARATOR);
        define('CORE_PID_DIR', CORE_WORK_ROOT . 'mainpid' . DIRECTORY_SEPARATOR);
        define('CORE_MYSQL_DRIVER', 'mysqli');
        if (file_exists(CORE_WORK_ROOT . '.envkey')) {
            $env = trim(file_get_contents(CORE_WORK_ROOT . '.envkey'));
        } else {
            if ('cli' == php_sapi_name()) {
                $options = 'e:';
                $params = getopt($options);
                $env = $params['e'] ?? 'local';
            } else {
                $env = Request::server('HTTP_ENV') ?? 'local';
            }
        }

        //环境定义
        define('CORE_ENV', $env);

        //配置路径
        define('CORE_CONFIG_PATH', CORE_WORK_ROOT . 'conf' . DIRECTORY_SEPARATOR);
        if (file_exists(CORE_CONFIG_PATH . 'application_name')) {
            $name = trim(file_get_contents(CORE_CONFIG_PATH . 'application_name'));
        } else {
            $name = 'app';
        }
        define('CORE_NAME', $name);

        // consul相关配置定义
        $swooleConfig = Config::getSwooleServerConfig();
        $consulAgent = $swooleConfig["consul_agent"] ?? "";
        if(empty($consulAgent)){
            $consulAgent = trim(getenv("SQKB_CONSUL_AGENT"));
        }
        if(empty($consulAgent)){
            $consulAgent = "127.0.0.1:8500";
        }
        define('CORE_CONSUL_AGENT', $consulAgent);

        // 是否从consul加载配置
        $needLoadConfigFromConsul = false;
        $processConfig = $swooleConfig["process_config"] ?? "";
        if (!empty($processConfig)) {
            $process = explode(",", $processConfig);
            if (!empty($process)) {
                foreach ($process as $tmp) {
                    list($_, $num, $handler) = explode(":", $tmp);
                    if (intval($num) > 0 && "config" == trim($handler)) {
                        $needLoadConfigFromConsul = true;
                        break;
                    }
                }
            }
        }
        define('CORE_USE_CONSUL_CONFIG', $needLoadConfigFromConsul);
    }
}
